package com.sfd.core.module;

import com.sfd.core.constant.ResCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 接口返回数据格式
 *
 * @author scott
 * @email jeecgos@163.com
 * @date 2019年1月19日
 */
@Getter
@AllArgsConstructor
@Accessors(chain = true)
public class Result<T> implements Serializable {

    protected static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    protected boolean success = true;

    /**
     * 返回处理消息
     */
    protected String message;

    /**
     * 返回代码
     * <p>
     * " 1000: 正常" +
     * "；1001: 业务异常" +
     * "；1002: 参数异常" +
     * "；1003: 系统异常" +
     * "；1004: 调用其他平台接口异常" +
     * "；1005: 请求地址异常" +
     * "；1006: 无效登录状态" +
     * "；1007: 权限不足" +
     * "；1008: 账号被禁用"
     */
    protected Integer code;

    /**
     * 返回数据对象 data
     */
    protected T result;

    /**
     * 时间戳
     */
    protected long timestamp = System.currentTimeMillis();

    protected Result() {

    }

    /**
     * 操作成功
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> success() {
        return success(ResCodeEnum.CODE_SUCCESS.getMsg(), null);
    }

    /**
     * 操作成功
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(String msg) {
        return success(msg, null);
    }

    /**
     * 操作成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data) {
        return success(ResCodeEnum.CODE_SUCCESS.getMsg(), data);
    }

    /**
     * 操作成功
     *
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(String msg, T data) {
        return result(true, ResCodeEnum.CODE_SUCCESS.getValue(), msg, data);
    }

    /**
     * 参数异常
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> paramError() {
        ResCodeEnum codeSysErr = ResCodeEnum.CODE_PARAM_ERR;
        return result(codeSysErr.getValue(), codeSysErr.getMsg());
    }

    /**
     * 参数异常
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> paramError(String message) {
        ResCodeEnum codeSysErr = ResCodeEnum.CODE_PARAM_ERR;
        return result(codeSysErr.getValue(), message);
    }

    /**
     * 系统异常
     *
     * @return
     */
    public static <T> Result<T> sysError() {
        ResCodeEnum codeSysErr = ResCodeEnum.CODE_SYS_ERR;
        return result(codeSysErr.getValue(), codeSysErr.getMsg());
    }

    /**
     * 系统异常
     *
     * @param msg
     * @return
     */
    public static <T> Result<T> sysError(String msg) {
        ResCodeEnum codeSysErr = ResCodeEnum.CODE_SYS_ERR;
        return result(codeSysErr.getValue(), msg);
    }

    /**
     * 业务异常
     *
     * @return
     */
    public static <T> Result<T> busError() {
        ResCodeEnum codeSysErr = ResCodeEnum.CODE_BUS_ERR;
        return result(codeSysErr.getValue(), codeSysErr.getMsg());
    }

    /**
     * 业务异常
     *
     * @param msg
     * @return
     */
    public static <T> Result<T> busError(String msg) {
        ResCodeEnum codeSysErr = ResCodeEnum.CODE_BUS_ERR;
        return result(codeSysErr.getValue(), msg);
    }

    public static <T> Result<T> busError(String msg, T data) {
        ResCodeEnum codeSysErr = ResCodeEnum.CODE_BUS_ERR;
        return result(codeSysErr.getValue(), msg, data);
    }

    /**
     * 自定义结果
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Result<T> result(Integer code, String msg) {
        return result(code, msg, null);
    }

    /**
     * 自定义结果
     *
     * @param code
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> result(Integer code, String msg, T data) {
        return new Result<T>(false, msg, code, data, System.currentTimeMillis());
    }

    /**
     * 自定义结果
     *
     * @param code
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> result(boolean success, Integer code, String msg, T data) {
        return new Result<T>(success, msg, code, data, System.currentTimeMillis());
    }

    /**
     * 无权限访问返回结果
     */
    public static <T> Result<T> noauth() {
        ResCodeEnum codeLoginErr = ResCodeEnum.CODE_AUTHORIZATION_ERR;
        return result(codeLoginErr.getValue(), codeLoginErr.getMsg());
    }

    /**
     * 无权限访问返回结果
     */
    public static <T> Result<T> noauth(String msg) {
        ResCodeEnum codeLoginErr = ResCodeEnum.CODE_AUTHORIZATION_ERR;
        return result(codeLoginErr.getValue(), msg);
    }

    /**
     * 无token、登录状态错误
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> loginErr() {
        ResCodeEnum codeEnum = ResCodeEnum.CODE_LOGIN_ERR;
        return result(false, codeEnum.getValue(), codeEnum.getMsg(), null);
    }

    public boolean isSuccess() {
        return ResCodeEnum.CODE_SUCCESS.getValue().equals(this.getCode()) || 200 == this.getCode();
    }

}
